#ifndef __ERROR_HANDLE_H__
#define __ERROR_HANDLE_H__
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

enum ERRORS {
    ERR_UNIMP = 1,
    ERR_UNKNOWN,
};

#define ERRBUF_SIZE 256
static char err_msg[ERRBUF_SIZE];
#define check(x)                                                               \
    do {                                                                       \
        if (!(x)) {                                                            \
            snprintf(err_msg, ERRBUF_SIZE,                                     \
                     "[neodocker]: error while checking " #x " in " __FILE__   \
                     ": %d",                                                   \
                     __LINE__);                                                \
            perror(err_msg);                                                   \
            exit(errno);                                                       \
        }                                                                      \
    } while (0)

#define if_fail_return(x)                                                      \
    do {                                                                       \
        int rt;                                                                \
        if ((rt = (x)) == -1) {                                                \
            return rt;                                                         \
        }                                                                      \
    } while (0)

#define if_fail(condition, fail_path)                                          \
    do {                                                                       \
        if (!(condition)) {                                                    \
            snprintf(err_msg, ERRBUF_SIZE,                                     \
                     "[neodocker]: error while checking " #condition           \
                     " in " __FILE__ ": %d",                                   \
                     __LINE__);                                                \
            perror(err_msg);                                                   \
            fail_path;                                                         \
            exit(errno);                                                       \
        }                                                                      \
    } while (0)

#endif // !__ERROR_HANDLE_H__
